﻿// JScript 文件
//初始化
    function GetTableInfo(tableObj)
    {
        if(tableObj==null)
        {
            return null;
        }
        var allCells = new Array();
        
        //根据表的内容初始化 allCells
//        var tableObj  = document.getElementById("datatable");
        var rowNum = tableObj.rows.length;
        for (i=0; i < rowNum; i++)               //遍历每行
        {
            var tr = tableObj.rows[i];
            var cellNum = tr.cells.length;
            var startCell = 0;
            for(j=0;j<cellNum;j++)  //每个单元格
            {
                var td = tableObj.rows[i].cells[j];
                var tempStart = startCell;
                // 根据每个单元格跨行，列的情况重新进行序号定义
                for(y=tr.rowIndex;y<tr.rowIndex+td.rowSpan;y++)
                {
                    startCell = tempStart;
                    for(x=0;x<td.colSpan;x++)
                    {
                        if(allCells[y]==null)
                        {
                            allCells[y] = new Array();
                        }
                        while(allCells[y][startCell]!=null)
                        {
                            startCell++;
                        }
                        var inputs = td.getElementsByTagName("input");
                        if(inputs.length>0)
                        {

                            for(var inum=0;inum<inputs.length;inum++)
                            {
                                if( inputs[inum].type == "text")
                                {
                                    allCells[y][startCell] ={Sign: i+','+j,Text:inputs[inum].value,
                                    Align:td.style.textAlign=="" ? "center":td.style.textAlign,
                                    Type:td.style.textAlign=="right"?"number":"text"};
                                }
                            }
                        }
                        else
                        {
                            inputs = td.getElementsByTagName("span");
                            if(inputs.length>0)
                            {
                                var tempt = inputs[0].getAttribute("title");
                                allCells[y][startCell] ={Sign: i+','+j,Text:tempt,
                                Align:td.style.textAlign=="" ? "center":td.style.textAlign,
                                Type:td.style.textAlign=="right"?"number":"text"};
                            }
                            else
                            {
                                inputs = td.getElementsByTagName("select");
                                if(inputs.length>0)
                                {
                                     var tempt = inputs[0].options[inputs[0].selectedIndex].text;
                                     allCells[y][startCell] ={Sign: i+','+j,Text:tempt,
                                        Align:td.style.textAlign=="" ? "center":td.style.textAlign,
                                        Type:td.style.textAlign=="right"?"number":"text"};
                                }
                                else
                                {
                                    //Sign 行列单元格的标记，所属TD
                                    allCells[y][startCell] ={Sign: i+','+j,Text:td.innerText,
                                    Align:td.style.textAlign=="" ? "center":td.style.textAlign,
                                    Type:td.style.textAlign=="right"?"number":"text"};
                                }
                            }
                        }
                        if(td.rect == null)
                        {
                            tempStart = startCell;
                            td.rect = {left:startCell,top:y,right:startCell,botton:y};
                        }

                    }
                }
            }
        }
//        //打印调试信息
//        var showdt = document.getElementById('debugView');
//        for(i=0;i<allCells.length;i++)
//        {
//            var lastIndex = showdt.rows.length;
//            showdt.insertRow(lastIndex);
//            for(j=0;j<allCells[i].length;j++)
//            {
//                showdt.rows[lastIndex].insertCell(j);
//                showdt.rows[lastIndex].cells[j].innerHTML=allCells[i][j].Sign +"&nbsp;:"+allCells[i][j].Text+"&nbsp;："+allCells[i][j].Align+"&nbsp;:"+allCells[i][j].Type;
//            }
//        }


        return allCells;
    }
    
        //table ： 表对象，col：需要排序的列index(0开始)，asc：正序true，top：上几行不排序，buttom下几行不排序
        // SortTableCell(document.getElementById("aa"),3,true,2,1);
        // SortTableCell(document.getElementById("aa"),3,false,2,1);
        function SortTableCell(table,col,asc,top,buttom)
        {
            if(table!=null)
            {
                var tempindex = new Array();
                if(table!=null)
                {
                    var index = 0;
                    for(var q =top ; q < table.rows.length-buttom;q++)
                    {
                        tempindex[index] =parseFloat(table.rows[q].cells[col].innerText == "" ? -1: table.rows[q].cells[col].innerText);
                       index++;
                    }                
                    for(var i = 0; i < tempindex.length;i++)
                    {
                        var tti = i;
                        //正序
                        if(asc)
                        {
                            tti = GetMinCell(tempindex,i);
                        }
                        else
                        {
                            tti = GetMaxCell(tempindex,i);
                        }
                        ttti = tempindex[tti];
                        tttii = tempindex[i];
                        for(var j=i;j<=tti;j++)
                        {
                            if(j!=i)
                            {
                                ttti = tttii;
                                tttii = tempindex[j];
                                tempindex[j] = ttti;
                            }
                            else
                            {
                                tttii = tempindex[j];
                                tempindex[j] = ttti;
                            }
                        }
                        table.moveRow(tti+top,i+top );
                    }
                }
            }
        }

        function GetMaxCell(tarray,ain)
        {
            maxMan = ain;
            for(var i = ain+1; i < tarray.length;i++)
            {
                if(tarray[maxMan]<=tarray[i])
                {
                    maxMan=i;
                }
            }
            return maxMan;
        }
        function GetMinCell(tarray,din)
        {
            var minain =din;
            for(var i = din+1; i < tarray.length;i++)
            {
                if(tarray[minain]>=tarray[i])
                {
                    minain=i;
                }
            }
            return minain;
        }

//table ： 表对象，col：需要排序的列index(0开始)，asc：正序true，top：上几行不排序，buttom下几行不排序, noIndex不参与排序的列
// SortTableCell(document.getElementById("aa"),3,true,2,1);
// SortTableCell(document.getElementById("aa"),3,false,2,1);
function SortTableCell(table, col, asc, top, buttom, noIndex) {
    if (table != null) {
        var tempindex = new Array();
        var noIndexCellValue = new Array();
        var index = 0;

        for (var q = top; q < table.rows.length - buttom; q++) {
            tempindex[index] = parseFloat(table.rows[q].cells[col].innerText == "" ? -1 : table.rows[q].cells[col].innerText);
            noIndexCellValue[index] = table.rows[q].cells[noIndex].innerText;
            index++;
        }

        for (var i = 0; i < tempindex.length; i++) {
            var tti = i;
            //正序
            if (asc) {
                tti = GetMinCell(tempindex, i);
            }
            else {
                tti = GetMaxCell(tempindex, i);
            }
            ttti = tempindex[tti];
            tttii = tempindex[i];
            for (var j = i; j <= tti; j++) {
                if (j != i) {
                    ttti = tttii;
                    tttii = tempindex[j];
                    tempindex[j] = ttti;
                }
                else {
                    tttii = tempindex[j];
                    tempindex[j] = ttti;
                }
            }
            table.moveRow(tti + top, i + top);
        }

        index = 0;
        for (q = top; q < table.rows.length - buttom; q++) {
            table.rows[q].cells[noIndex].innerText = noIndexCellValue[index];
            index++;
        }
    }
}